package com.guo.hui.shiro;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.guo.hui.controller.BaseController;
import com.guo.hui.dao.shiro.TpPermissionMapper;
import com.guo.hui.dao.shiro.TpRoleMapper;
import com.guo.hui.dao.shiro.TpUserMapper;
import com.guo.hui.enumtype.DelFlag;
import com.guo.hui.model.shiro.TpPermission;
import com.guo.hui.model.shiro.TpRole;
import com.guo.hui.model.shiro.TpUser;
import com.guo.hui.page.PageParam;
import com.guo.hui.page.PageResult;
import com.guo.hui.service.PermissionService;
import com.guo.hui.service.RoleService;
import com.guo.hui.service.TpUserShiroService;
import com.guo.hui.util.UtilJson;

@Controller
@RequestMapping("permission")
public class PermissionController extends BaseController {

	@Autowired
	private PermissionService permisService;
	@Autowired
	private TpPermissionMapper permisDao;

	@RequestMapping("/permission")
	public String index() {
		return "blog/permission/permission";
	}

	@ResponseBody
	@RequestMapping("/permissiondata")
	public String permissiondata(Model mm) {
		List<TpPermission> list = permisService.selectByMap(new HashMap<String, Object>());
		return UtilJson.toJson(list);
	}

	@ResponseBody
	@RequestMapping("/permissiondatalist")
	public String permissiondatalist(HttpServletRequest request, String map) {
		Map<String, Object> parms = new HashMap<String, Object>();
		parms = UtilJson.fromJsonType(map, parms.getClass());
		PageParam pageParm = new PageParam();
		pageParm.setPage(Integer.valueOf(request.getParameter("page")));// 页数
		pageParm.setPageSize(Integer.valueOf(request.getParameter("rows")));// 条数
		pageParm.setSearchContent(parms);
		PageInfo<TpPermission> pagelist = permisService.selectByMapPage(pageParm);
		PageResult result = new PageResult();
		result.setTotal((int) pagelist.getTotal());
		result.setRows(pagelist.getList());
		return UtilJson.toJson(result);
	}

	@RequestMapping("/permissionadd")
	public String permissionadd() {
		return "blog/permission/permissionadd";
	}

	@ResponseBody
	@RequestMapping("/permissionsave")
	public String permissionsave(@RequestParam String permis) {
		TpPermission tp = (TpPermission) UtilJson.fromJson(permis, TpPermission.class);
		permisService.savePermission(tp);
		return "ok";
	}

	@ResponseBody
	@RequestMapping("/permissiondelete")
	public String permissiondelete(@RequestParam int id) {
		TpPermission tp = new TpPermission();
		tp.setId(id);
		tp.setDelflag(DelFlag.LOSE.toString());
		permisService.updatePermission(tp);
		return "ok";
	}

	@RequestMapping("/update")
	public String update(Model mm, @RequestParam int id) {
		TpPermission tp = permisDao.selectById(id);
		mm.addAttribute("tp", tp);
		return "blog/permission/update";
	}

	@ResponseBody
	@RequestMapping("/permissionupdate")
	public String permissionupdate(@RequestParam String tp) {
		TpPermission permis = (TpPermission) UtilJson.fromJson(tp, TpPermission.class);
		permisService.updatePermission(permis);
		return "ok";
	}

	@ResponseBody
	@RequestMapping("/rolepermis")
	public String rolepermis(@RequestParam Integer roleid, String ids) {
		System.out.println(roleid);
		Integer[] permids = (Integer[]) UtilJson.fromJson(ids, Integer[].class);
		permisService.saveRolePermis(roleid, permids);
		return "ok";
	}

}
